package org.zero.concurrency.demo.example.count;

import lombok.extern.slf4j.Slf4j;
import org.zero.concurrency.demo.annotations.NotThreadSafe;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @program: concurrency-demo
 * @description: 模拟并发执行
 * @author: 01
 * @create: 2018-10-14 00:01
 **/
@Slf4j
@NotThreadSafe
public class CountExample3 {
    /**
     * 计数
     */
    private static volatile int count = 0;

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < 5000; i++) {
            executorService.execute(CountExample3::add);
        }

        executorService.shutdown();
        log.info("count: {}", count);
    }

    private synchronized static void add() {
        count++;
    }
}
